package com.inspur.his.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inspur.his.constant.Constant;
import com.inspur.his.pojo.Mem;
import com.inspur.his.pojo.Pay;
import com.inspur.his.pojo.User;
import com.inspur.his.service.MemService;
import com.inspur.his.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Controller
public class MemController {
    @Autowired
    MemService memService;
    @Autowired
    PayService payService;

    @RequestMapping("memListController")
    public String memList(HttpServletRequest request, Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        //获取页面提交来的查询条件
        String name = request.getParameter("name");
        String memberid = request.getParameter("memberid");
        //  点击用户管理左侧菜单进行默认无条件查询
        HashMap map = new HashMap();
        map.put("name", name);
        map.put("memberid", memberid);
        PageHelper.startPage(pn, 5);
        List<Mem> memList = memService.memList(map);
        PageInfo<Mem> pageInfo = new PageInfo<Mem>(memList, memList.size());
        model.addAttribute("memList", memList);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("name", name);
        model.addAttribute("memberid", memberid);
        return "mem/memList.jsp";
    }

        @RequestMapping("memAddController")
        public String memAdd(Mem mem, Model model,HttpServletRequest req) throws Exception {
            //参数绑定 user对象中已经绑定了表单中的用户信,息(确认sql中插入的所有字段均有值吗？）
            //思考主键的生成方式--多种1 MySQL中的主键自增
            String id = UUID.randomUUID().toString();
            mem.setMemberid(id);
            Date day = new Date();
            SimpleDateFormat fm = new SimpleDateFormat("yyyyMMdd");
            String today = fm.format(day);
            mem.setMemberid(today + String.valueOf(day.getTime()));
            mem.setCreatedate(day);
            mem.setStatus(Constant.USER_STATUS_ABLE);
            memService.memAdd(mem);
            //同时需要往缴费明细表插入一条数据
           /* Pay pay = new Pay();
            pay.setMemberid(id);
            pay.setBalance(mem.getBalance());
            pay.setType(mem.getType());
            pay.setMoney(mem.getBalance());
            pay.setDate(day);
            HttpSession session =req.getSession();
            User user = (User)session.getAttribute("suser");
            pay.setMember(user.getId());
            payService.addPay(pay);
*/

            model.addAttribute("mem", mem);
            model.addAttribute("flag", Constant.USER_STATUS_ABLE);
            model.addAttribute("success", "会员信息保存成功！");
            return "mem/memAdd.jsp";
        }
            @RequestMapping("modifyMemController")
            public String modifyMem(String id,Model model) {
                Mem mem = memService.getMemById(id);
                model.addAttribute("mem",mem);
                return "/mem/memModify.jsp";
            }


            @RequestMapping("memUpdateController")
            public String memupdate(Mem mem, Model model) {
                memService.updateMem(mem);
                model.addAttribute("success", "会员信息修改成功");
                model.addAttribute("mem", mem);
                model.addAttribute("flag", "1");
                return "mem/memModify.jsp";
            }

    @RequestMapping("/memczController")
    public  String memcz(String id,Model model) {
        Mem mem = memService.getMemById(id);
        model.addAttribute("mem",mem);
        model.addAttribute("flag","1");
        return "mem/memcz.jsp";
    }

    @RequestMapping("memUpdateczController")
    public String memupdatecz(Mem mem, Model model) {
        memService.updateczMem(mem);
        model.addAttribute("mem", mem);
        model.addAttribute("flag", "1");
        return "redirect:memListController";
    }
    @RequestMapping("memchangeStatus")
    public String memchangeStatus(HttpServletRequest req, Model md) {
        // 获取页面传递过来的查询条件
        String name = req.getParameter("name");
        String role = req.getParameter("role");
        // 页面查询条件查询后锁定显示在页面上
        md.addAttribute("name", name);

        String memberid = req.getParameter("memberid");
        String sts = req.getParameter("sts");
        HashMap map = new HashMap<String, String>();
        map.put("memberid", memberid);
        map.put("status", sts);
        memService.memchangeStatus(map);
        return "redirect:memListController";
    }



    @RequestMapping("memList")
    @ResponseBody
    public List<Mem> deptInfo(HttpServletRequest req,Model md) {
        HashMap map = new HashMap<String, String>();
        String departid =req.getParameter("departid");
        map.put("departid",departid);
        List<Mem> memList = memService.memList(map);
        md.addAttribute("deptList", memList);
        return memList;
    }

}
